home *** CD-ROM | disk | FTP | other *** search
- /*
-
- simple tpoly-to-obj converter - 6/14/93 by Francisco X DeJesus
-
- dejesus@c3ot.saic.com - dejesus@avalon.chinalake.navy.mil
-
- (written under SunOS 4.1.3... tweaks may be necessary for the
- date/time stuff to work, but it is not crucial, so they can be
- commented out and a fake date hard-coded if neccesary).
-
- This program is provided "as-is" (ie: use it at your own risk!).
- Permission is hereby granted to redistribute this program freely,
- provided that this header is left unmodified. If you find any bugs or
- modify the program to improve it in any way, please let me know...
-
- If you have any interesting 3D objects/models, in any format, please
- upload them to the anonymous ftp archive avalon.chinalake.navy.mil!!!
-
- */
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/timeb.h>
- #include <sys/time.h>
- #include <strings.h>
-
- static char *months[]=
- {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
- };
-
- static char *days[]=
- {
- "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
- };
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- FILE *fpn,*fpo;
- struct timeval now;
- struct timezone zone;
- time_t then;
- struct tm *t;
- char infilename[40];
- char outfilename[40];
- int i, v, vtotal, faces;
- float x,y,z;
-
- if (argc == 1)
- {
- printf("Tpoly file to be converted? ");
- scanf("%s",infilename);
- printf("Obj file to output? ");
- scanf("%s",outfilename);
- if ((fpn = fopen(infilename,"r")) == 0) {
- printf ("file does not exist: %s\n", infilename);
- exit ();
- }
- if ((fpo = fopen(outfilename,"w")) == 0) {
- printf ("file cannot be created: %s\n", outfilename);
- exit ();
- }
- }
- else if (argc == 2)
- {
- if (strcmp (argv[1], "-h") == 0) {
- printf("usage:\n");
- printf(" tpoly2obj [inputfile.tpoly [outputfile.obj]]\n");
- printf("Please use full filenames. If files are not speficied on the\n");
- printf("command line, the program will prompt you for them.\n");
- exit();
- }
- printf("Obj file to output? ");
- scanf("%s",outfilename);
- if ((fpn = fopen(argv[1],"r")) == 0) {
- printf ("file does not exist: %s\n", argv[1]);
- exit ();
- }
- if ((fpo = fopen(outfilename,"w")) == 0) {
- printf ("file cannot be created: %s\n", outfilename);
- exit ();
- }
- }
- else
- {
- if ((fpn = fopen(argv[1],"r")) == 0) {
- printf ("file does not exist: %s\n", argv[1]);
- exit ();
- }
- if ((fpo = fopen(argv[2],"w")) == 0) {
- printf ("file cannot be created: %s\n", argv[2]);
- exit ();
- }
- }
-
- /* convert! */
-
- vtotal=0;
-
- if (gettimeofday(&now,&zone)<0)
- perror("gettimeofday failed"), exit(2);
-
- if ((t=localtime(&now))==NULL)
- perror("localtime failed"), exit(3);
-
- if (t->tm_mday > 9)
- fprintf(fpo,"# %s %s %2.2d %2.2d:%2.2d:%2.2d %4.4d\n",
- days[t->tm_wday],months[t->tm_mon],t->tm_mday,
- t->tm_hour,t->tm_min,t->tm_sec,
- (1900+t->tm_year));
- if (t->tm_mday < 10)
- fprintf(fpo,"# %s %s %d %2.2d:%2.2d:%2.2d %4.4d\n",
- days[t->tm_wday],months[t->tm_mon],t->tm_mday,
- t->tm_hour,t->tm_min,t->tm_sec,
- (1900+t->tm_year));
-
- fprintf(fpo,"#\n");
- fprintf(fpo,"# Object converted by tpoly2obj\n",infilename);
- fprintf(fpo,"#\n\n");
- fprintf(fpo,"g\n");
-
- printf("Starting conversion...\n");
-
- while (!feof(fpn))
- {
- fscanf(fpn,"%d",&v);
- if (v != 3) {
- break;
- }
- vtotal++;
- v=0;
-
- /* three vertices because each face is a triangle */
-
- fscanf(fpn,"%f %f %f",&x,&y,&z);
- fprintf(fpo,"v %f %f %f\n",x,y,z);
-
- fscanf(fpn,"%f %f %f",&x,&y,&z);
- fprintf(fpo,"v %f %f %f\n",x,y,z);
-
- fscanf(fpn,"%f %f %f",&x,&y,&z);
- fprintf(fpo,"v %f %f %f\n",x,y,z);
- }
-
- faces=vtotal;
- vtotal=vtotal*3;
-
- printf("%d vertices\n",vtotal);
-
- fprintf(fpo,"# %d vertices\n\n",vtotal);
-
- fprintf(fpo,"# 0 texture vertices\n\n");
-
- fprintf(fpo,"# 0 normals\n\n");
-
- fprintf(fpo,"g default\n");
-
-
- for (i=0;i<faces;i++) {
- v=i*3;
- fprintf (fpo,"f %d %d %d\n",(v+1),(v+2),(v+3));
- }
-
- fprintf(fpo,"# %d elements\n\n",faces);
-
- fclose(fpn);
- fclose(fpo);
- }
-